package com.service.healthycode.controller;

import com.service.config.utils.JsonXMLUtils;
import com.service.config.utils.RedisUtil;
import com.service.config.utils.Result;
import com.service.config.utils.UserTokenManager;
import com.service.healthycode.dto.DangerUserDto;
import com.service.healthycode.entity.UserLoginRecords;
import com.service.healthycode.service.AdminService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import java.util.List;

import static com.service.config.constant.Constant.USER_NOT_LOGIN;

/**
 * @author: hjl
 * @date: 2022/04/13 14:14
 * @description
 */
@CrossOrigin
@RestController
@RequestMapping("/admin")
@Api(value = "管理员 ")
public class AdminController {
    @Resource
    private AdminService adminService;
    @Resource
    private RedisUtil redisUtil;


    @ApiOperation(value = "上传风险人员")
    @RequestMapping(value = "/addDangerUser",method = RequestMethod.POST,produces = "application/json;charset=utf-8")
    @ResponseBody
    public Result addDangerUser(@RequestBody DangerUserDto dangerUserDto,
                                @RequestHeader(value = "cloud-Auth-Token") @ApiParam("token") String cloudAuthToken) throws Exception {
        if (cloudAuthToken != null && !cloudAuthToken.equals("")) {
            String userId = UserTokenManager.getUserId(cloudAuthToken);
            if (redisUtil.exist(cloudAuthToken + userId)) {
                String value = redisUtil.getValue(cloudAuthToken + userId);
                UserLoginRecords userDto = JsonXMLUtils.json2obj(value, UserLoginRecords.class);
                if (userDto.getRoleId()==2)
                {
                    return adminService.addDangerUser(dangerUserDto,userDto);
                }
                return Result.failure(10005,"用户权限不足");
            } else {
                return Result.failure(10004, "登录信息已失效");
            }
        }
        return Result.failure(USER_NOT_LOGIN, "用户未登录");
    }

    @ApiOperation(value = "查询所有上传的风险人员")
    @RequestMapping(value = "/queryUploadRiskUser",method = RequestMethod.POST,produces = "application/json;charset=utf-8")
    @ResponseBody
    public Result queryUploadRiskUser(@RequestHeader(value = "cloud-Auth-Token") @ApiParam("token") String cloudAuthToken) throws Exception {
        if (cloudAuthToken != null && !cloudAuthToken.equals("")) {
            String userId = UserTokenManager.getUserId(cloudAuthToken);
            if (redisUtil.exist(cloudAuthToken + userId)) {
                String value = redisUtil.getValue(cloudAuthToken + userId);
                UserLoginRecords userDto = JsonXMLUtils.json2obj(value, UserLoginRecords.class);
                if (userDto.getRoleId()==2)
                {
                    return adminService.queryUploadRiskUser();
                }
                return Result.failure(10005,"用户权限不足");
            } else {
                return Result.failure(10004, "登录信息已失效");
            }
        }
        return Result.failure(USER_NOT_LOGIN, "用户未登录");
    }

    @ApiOperation(value = "查询风险人员")
    @RequestMapping(value = "/queryTouchUser",method = RequestMethod.POST,produces = "application/json;charset=utf-8")
    @ResponseBody
    public Result queryTouchUser(@RequestBody DangerUserDto dangerUserDto,
                                @RequestHeader(value = "cloud-Auth-Token") @ApiParam("token") String cloudAuthToken) throws Exception {
        if (cloudAuthToken != null && !cloudAuthToken.equals("")) {
            String userId = UserTokenManager.getUserId(cloudAuthToken);
            if (redisUtil.exist(cloudAuthToken + userId)) {
                String value = redisUtil.getValue(cloudAuthToken + userId);
                UserLoginRecords userDto = JsonXMLUtils.json2obj(value, UserLoginRecords.class);
                if (userDto.getRoleId()==2)
                {
                    return adminService.queryTouchUser(dangerUserDto,userId);
                }
                return Result.failure(10005,"用户权限不足");
            } else {
                return Result.failure(10004, "登录信息已失效");
            }
        }
        return Result.failure(USER_NOT_LOGIN, "用户未登录");
    }

    @ApiOperation(value = "删除风险人员")
    @RequestMapping(value = "/delDangerUser",method = RequestMethod.POST,produces = "application/json;charset=utf-8")
    @ResponseBody
    public Result delDangerUser(@RequestParam("dangerUserId")String dangerUserId,
                                @RequestHeader(value = "cloud-Auth-Token") @ApiParam("token") String cloudAuthToken) throws Exception {
        if (cloudAuthToken != null && !cloudAuthToken.equals("")) {
            String userId = UserTokenManager.getUserId(cloudAuthToken);
            if (redisUtil.exist(cloudAuthToken + userId)) {
                String value = redisUtil.getValue(cloudAuthToken + userId);
                UserLoginRecords userDto = JsonXMLUtils.json2obj(value, UserLoginRecords.class);
                if (userDto.getRoleId()==2)
                {
                    return adminService.delDangerUser(dangerUserId);
                }
                return Result.failure(10005,"用户权限不足");
            } else {
                return Result.failure(10004, "登录信息已失效");
            }
        }
        return Result.failure(USER_NOT_LOGIN, "用户未登录");
    }

    @ApiOperation(value = "修改风险人员信息")
    @RequestMapping(value = "/updateDangerUser",method = RequestMethod.POST,produces = "application/json;charset=utf-8")
    @ResponseBody
    public Result updateDangerUser(@RequestBody DangerUserDto dangerUserDto,
                                @RequestHeader(value = "cloud-Auth-Token") @ApiParam("token") String cloudAuthToken) throws Exception {
        if (cloudAuthToken != null && !cloudAuthToken.equals("")) {
            String userId = UserTokenManager.getUserId(cloudAuthToken);
            if (redisUtil.exist(cloudAuthToken + userId)) {
                String value = redisUtil.getValue(cloudAuthToken + userId);
                UserLoginRecords userDto = JsonXMLUtils.json2obj(value, UserLoginRecords.class);
                if (userDto.getRoleId()==2)
                {
                    return adminService.updateDangerUser(dangerUserDto,userDto);
                }
                return Result.failure(10005,"用户权限不足");
            } else {
                return Result.failure(10004, "登录信息已失效");
            }
        }
        return Result.failure(USER_NOT_LOGIN, "用户未登录");
    }


    @ApiOperation(value = "修改风险人员状态")
    @RequestMapping(value = "/updateDangerUserStatus",method = RequestMethod.GET,produces = "application/json;charset=utf-8")
    @ResponseBody
    public Result updateDangerUserStatus(@RequestParam("status") Integer status,
                                   @RequestParam("terminalMac") String terminalMac,
                                   @RequestParam("origin") Integer origin,
                                   @RequestParam("flag") Boolean flag,
                                   @RequestHeader(value = "cloud-Auth-Token") @ApiParam("token") String cloudAuthToken) throws Exception {
        if (cloudAuthToken != null && !cloudAuthToken.equals("")) {
            String userId = UserTokenManager.getUserId(cloudAuthToken);
            if (redisUtil.exist(cloudAuthToken + userId)) {
                String value = redisUtil.getValue(cloudAuthToken + userId);
                UserLoginRecords userDto = JsonXMLUtils.json2obj(value, UserLoginRecords.class);
                if (userDto.getRoleId()==2)
                {
                    return adminService.updateDangerUserStatus(origin,status,userDto,flag,terminalMac);
                }
                return Result.failure(10005,"用户权限不足");
            } else {
                return Result.failure(10004, "登录信息已失效");
            }
        }
        return Result.failure(USER_NOT_LOGIN, "用户未登录");
    }
}
